package top.tenebrous.yzl.chat.controller.chat;

import cn.easyes.core.biz.SAPageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.easyes.core.toolkit.EsWrappers;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.tenebrous.yzl.basic.exceptions.BizException;
import top.tenebrous.yzl.basic.model.YzlChatResult;
import top.tenebrous.yzl.chat.controller.BaseController;
import top.tenebrous.yzl.chat.domain.member.MMemUser;
import top.tenebrous.yzl.chat.domain.es.EsMemChatRecord;
import top.tenebrous.yzl.chat.mapper.member.MMemUserMapper;
import top.tenebrous.yzl.chat.mapper.es.EsMemChatRecordMapper;
import top.tenebrous.yzl.chat.model.ChatDTO;
import top.tenebrous.yzl.chat.model.MemberLoginInfo;
import top.tenebrous.yzl.im.context.ImSender;
import top.tenebrous.yzl.im.message.MessageBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @Classname ChatController
 * @Description TODO
 * @Date 2023/11/23 12:30 AM
 * @Created by zhangya
 */
@Slf4j
@RestController
@RequestMapping("/v1/chat")
public class ChatController extends BaseController {

  @Autowired
  private MMemUserMapper memUserMapper;

  @Autowired
  private ImSender imSender;

  @Resource
  private EsMemChatRecordMapper esMemChatRecordMapper;

  @PostMapping("/send")
  public YzlChatResult sendMessage(@RequestBody ChatDTO chatDTO) throws IOException {
    MemberLoginInfo userInfo = getUserInfo();
    MMemUser memUser = memUserMapper.selectById(chatDTO.getTo());
    if (Objects.isNull(memUser)) {
      throw new BizException("该用户不存在");
    }
    MessageBody messageBody = new MessageBody();
    messageBody.setFrom(String.valueOf(userInfo.getId()));
    messageBody.setTo(chatDTO.getTo());
    messageBody.setCreateTime(String.valueOf(System.currentTimeMillis()));
    messageBody.setMsgType("0");
    messageBody.setChatType(chatDTO.getChatType());
    messageBody.setGroupId(chatDTO.getGroupId());
    messageBody.setContent(chatDTO.getMessage());
    imSender.send(messageBody);
    return YzlChatResult.success();
  }

  @GetMapping("/list")
  public YzlChatResult<SAPageInfo<EsMemChatRecord>> getChatRecodePage(int pageSize, String list) {
    LambdaEsQueryWrapper<EsMemChatRecord> lambdaEsQueryWrapper = EsWrappers.lambdaQuery(EsMemChatRecord.class);
    lambdaEsQueryWrapper.size(10);
    // 必须指定一种排序规则,且排序字段值必须唯一 此处我选择用id进行排序 实际可根据业务场景自由指定,不推荐用创建时间,因为可能会相同
    List<Object> list1 = null;
    lambdaEsQueryWrapper.orderByDesc(EsMemChatRecord::getId);
    if (StringUtils.isNotBlank(list)) {
      list1 = JSON.parseArray(list, Object.class);
    }
    SAPageInfo<EsMemChatRecord> saPageInfo = esMemChatRecordMapper.searchAfterPage(lambdaEsQueryWrapper, list1, pageSize);
    // 第一页
    log.info("聊天记录查询:{}", JSON.toJSONString(saPageInfo));
    return YzlChatResult.success(saPageInfo);
  }
}
